// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UIWiringEditor.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Forge.UI.Forms
{
    #region Using Directives

    using System;

    using Org.OpenTrader.Framework.Forge.Interfaces;
    using Org.OpenTrader.Framework.LiveObjects;
    using Org.OpenTrader.Framework.LiveObjects.Interfaces;
    using Org.OpenTrader.Framework.UI.Forms;

    #endregion

    /// <summary>
    /// The ui wiring editor.
    /// </summary>
    /// <typeparam name="T">
    /// </typeparam>
    public partial class UIWiringEditor<T> : UIControl
        where T : Wiring, new()
    {
        #region Constants and Fields

        /// <summary>
        /// The wiring.
        /// </summary>
        private T wiring;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="UIWiringEditor{T}"/> class.
        /// </summary>
        public UIWiringEditor()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIWiringEditor{T}"/> class.
        /// </summary>
        /// <param name="wirables">
        /// The wirables.
        /// </param>
        /// <param name="w">
        /// The w.
        /// </param>
        public UIWiringEditor(IWire[] wirables, T w)
        {
            this.InitializeComponent();

            this.layersCombobox.Items.AddRange(wirables);
            this.Wiring = w;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets Wiring.
        /// </summary>
        public T Wiring
        {
            get
            {
                return this.wiring;
            }

            set
            {
                this.wiring = value;
                this.PrepareControl(true);
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// User is changing the wire
        /// </summary>
        /// <param name="sender">
        /// </param>
        /// <param name="e">
        /// </param>
        private void OnLayersComboboxSelectedIndexChanged(object sender, EventArgs e)
        {
            IConfiguration c = null;
            var wirableInfo = this.layersCombobox.SelectedItem as IWire;
            if (wirableInfo.ObjectConfigurationSchema != null)
            {
                // There is a configuration for this wiring (that is the wire needs some parameters)
                c = new Configuration(Guid.NewGuid(), wirableInfo.ObjectConfigurationSchema, Singleton<LiveObjectsRegistry>.Instance.CurrentLiveObjectContainer);
            }

            this.wiring = new T();
            (this.wiring as Wiring).PluginID = wirableInfo.PluginID;
            (this.wiring as Wiring).InstrumentID = Guid.Empty;
            if (typeof(T).Equals(typeof(FeedWiring)))
            {
                (this.wiring as FeedWiring).SubscriptionConfiguration = c;
            }
            else if (typeof(T).Equals(typeof(OrderWiring)))
            {
                (this.wiring as OrderWiring).OrderRoutingConfiguration = c;
            }
            else
            {
                throw new Exception("Wiring type unknown");
            }

            this.PrepareControl(false);
        }

        /// <summary>
        /// The prepare control.
        /// </summary>
        /// <param name="select">
        /// The select.
        /// </param>
        private void PrepareControl(bool select)
        {
            if (this.wiring != null)
            {
                if (select)
                {
                    foreach (var v in this.layersCombobox.Items)
                    {
                        var w = v as IWire;
                        if (w.PluginID == this.wiring.PluginID)
                        {
                            this.layersCombobox.SelectedText = v.ToString();
                            break;
                        }
                    }
                }

                if (typeof(T).Equals(typeof(FeedWiring)))
                {
                    this.configurationEditor.Configuration = (this.wiring as FeedWiring).SubscriptionConfiguration;
                }
                else if (typeof(T).Equals(typeof(OrderWiring)))
                {
                    this.configurationEditor.Configuration = (this.wiring as OrderWiring).OrderRoutingConfiguration;
                }
                else
                {
                    throw new Exception("Wiring type unknown");
                }
            }
            else
            {
                this.configurationEditor.Configuration = null;
                this.layersCombobox.SelectedItem = null;
            }
        }

        #endregion
    }
}